package com.qxsx.midi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.exam.ApplyActivity;
import com.qxsx.midi.activity.exam.MidiTeacherActivity;
import com.qxsx.midi.activity.home.AdvertisementActivity;
import com.qxsx.midi.activity.home.BuyListActivity;
import com.qxsx.midi.activity.home.CertificateSearchActivity;
import com.qxsx.midi.activity.home.ExamListActivity;
import com.qxsx.midi.activity.me.LoginActivity;
import com.qxsx.midi.adapter.ImgAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.DataManager;
import com.qxsx.midi.model.Binner;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.MyGallery;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Joney on 2015/8/31.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private CustomProgress customProgress;

    private LinearLayout ll_one, ll_two, ll_three, ll_four;


    private DrawerLayout mDrawerLayout;


    private LinearLayout leftMenuPanel;

    private LinearLayout rightMenuPanel;

    //左侧菜单
    private TextView tv_left_exam, tv_left_book, tv_left_apply, tv_left_cert, tv_left_teacher;
    //右侧菜单
    private TextView tv_right_exam, tv_right_book, tv_right_apply, tv_right_cert, tv_right_teacher;

    //首页的轮播组件
    private MyGallery gallery = null;
    private ArrayList<Binner> imgList;
    private ArrayList<ImageView> portImg;
    private int preSelImgIndex = 0;
    private LinearLayout ll_focus_indicator_container = null;
    private ImgAdapter adapter;
    String login_name;//用户名

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        findById(v);
        inItData();
        return v;
    }

    /**
     * 界面显示时再加载数据
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {

        }
    }

    /**
     * 加载数据
     */
    private void inItData() {
        customProgress = CustomProgress.show(getActivity(), "正在加载中....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("param", "no");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("201", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if ("200".equals(code.toString())) {
                    try {
                        imgList = new ArrayList<Binner>();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object1 = array.getJSONObject(i);
                            Binner binner = new Binner();
                            binner.set_id(object1.getString("id"));
                            binner.set_name(object1.getString("img_name"));
                            binner.set_type(object1.getString("img_type"));
                            binner.set_url(object1.getString("img_url"));
                            binner.set_contexturl(object1.getString("img_display_url"));
                            imgList.add(binner);
                        }
                        adapter.AddDtata(imgList);
                        InitFocusIndicatorContainer();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                }
                customProgress.dismiss();
            }
        });
        imgList = new ArrayList<Binner>();
        adapter = new ImgAdapter(getActivity(), imgList);
        gallery.setAdapter(adapter);
        gallery.setFocusable(true);
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int selIndex, long arg3) {
                if (imgList.size() != 0) {
                    selIndex = selIndex % imgList.size();
                    // 修改上一次选中项的背景
                    portImg.get(preSelImgIndex).setImageResource(R.mipmap.binner_white);
                    // 修改当前选中项的背景
                    portImg.get(selIndex).setImageResource(R.mipmap.binner_green);
                    preSelImgIndex = selIndex;
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.e("position", position + "");
                Intent intent = new Intent();
                intent.putExtra("_url", imgList.get(position % imgList.size()).get_contexturl());
                intent.setClass(getActivity(), AdvertisementActivity.class);
                startActivity(intent);

            }

        });


        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mDrawerLayout.setDrawerListener(new DrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                ll_one.setClickable(true);
                ll_two.setClickable(true);
                ll_three.setClickable(true);
                ll_four.setClickable(true);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        login_name = (String) SPUtils.get(AppContext.getContext(), "login_name", "");

    }

    private void InitFocusIndicatorContainer() {
        portImg = new ArrayList<ImageView>();
        for (int i = 0; i < imgList.size(); i++) {
            ImageView localImageView = new ImageView(getActivity());
            localImageView.setId(i);
            ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
            localImageView.setScaleType(localScaleType);
            LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            localImageView.setLayoutParams(localLayoutParams);
            localImageView.setPadding(0, 0, 8, 0);
            if (i == 0) {
                localImageView.setImageResource(R.mipmap.binner_green);
            } else {
                localImageView.setImageResource(R.mipmap.binner_white);
            }
            portImg.add(localImageView);
            this.ll_focus_indicator_container.addView(localImageView);
        }
    }

    private void findById(View v) {

        ll_focus_indicator_container = (LinearLayout) v.findViewById(R.id.ll_focus_indicator_container);
        gallery = (MyGallery) v.findViewById(R.id.gallery);

        ll_one = (LinearLayout) v.findViewById(R.id.ll_one);
        ll_one.setOnClickListener(this);
        ll_two = (LinearLayout) v.findViewById(R.id.ll_two);
        ll_two.setOnClickListener(this);
        ll_three = (LinearLayout) v.findViewById(R.id.ll_three);
        ll_three.setOnClickListener(this);
        ll_four = (LinearLayout) v.findViewById(R.id.ll_four);
        ll_four.setOnClickListener(this);


        mDrawerLayout = (DrawerLayout) v.findViewById(R.id.id_drawerLayout);
        tv_left_exam = (TextView) v.findViewById(R.id.tv_left_exam);
        tv_left_exam.setOnClickListener(this);
        tv_left_book = (TextView) v.findViewById(R.id.tv_left_book);
        tv_left_book.setOnClickListener(this);
        tv_left_apply = (TextView) v.findViewById(R.id.tv_left_apply);
        tv_left_apply.setOnClickListener(this);
        tv_left_cert = (TextView) v.findViewById(R.id.tv_left_cert);
        tv_left_cert.setOnClickListener(this);
        tv_left_teacher = (TextView) v.findViewById(R.id.tv_left_teacher);
        tv_left_teacher.setOnClickListener(this);


        tv_right_exam = (TextView) v.findViewById(R.id.tv_right_exam);
        tv_right_exam.setOnClickListener(this);
        tv_right_book = (TextView) v.findViewById(R.id.tv_right_book);
        tv_right_book.setOnClickListener(this);
        tv_right_apply = (TextView) v.findViewById(R.id.tv_right_apply);
        tv_right_apply.setOnClickListener(this);
        tv_right_cert = (TextView) v.findViewById(R.id.tv_right_cert);
        tv_right_cert.setOnClickListener(this);
        tv_right_teacher = (TextView) v.findViewById(R.id.tv_right_teacher);
        tv_right_teacher.setOnClickListener(this);


    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_one:
                ll_two.setClickable(false);
                ll_three.setClickable(false);
                ll_four.setClickable(false);
                mDrawerLayout.openDrawer(Gravity.RIGHT);
                SPUtils.put(AppContext.getContext(), "apply_major_id", DataManager.getManage().getMajorList2().get(0).getMajor_id().toString());
                SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
                SPUtils.put(AppContext.getContext(), "subject_name", "电吉他");
                break;
            case R.id.ll_two:
                ll_one.setClickable(false);
                ll_three.setClickable(false);
                ll_four.setClickable(false);
                mDrawerLayout.openDrawer(Gravity.RIGHT);
                SPUtils.put(AppContext.getContext(), "apply_major_id", DataManager.getManage().getMajorList2().get(1).getMajor_id().toString());
                SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
                SPUtils.put(AppContext.getContext(), "subject_name", "电贝司");
                break;
            case R.id.ll_three:
                ll_one.setClickable(false);
                ll_two.setClickable(false);
                ll_four.setClickable(false);
                mDrawerLayout.openDrawer(Gravity.LEFT);
                SPUtils.put(AppContext.getContext(), "apply_major_id", DataManager.getManage().getMajorList2().get(2).getMajor_id().toString());
                SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
                SPUtils.put(AppContext.getContext(), "subject_name", "爵士鼓");
                break;
            case R.id.ll_four:
                Toast.makeText(getActivity(),"频道建设中.....",Toast.LENGTH_SHORT).show();
//                ll_one.setClickable(false);
//                ll_two.setClickable(false);
//                ll_three.setClickable(false);
//                mDrawerLayout.openDrawer(Gravity.LEFT);
//                SPUtils.put(AppContext.getContext(), "apply_major_id", DataManager.getManage().getMajorList2().get(3).getMajor_id().toString());
//                SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
//                SPUtils.put(AppContext.getContext(), "subject_name", "音乐理论");
                break;
            case R.id.tv_left_exam:
                if (login_name != "") {
                    startActivity(new Intent(getActivity(), ExamListActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }

                break;
            case R.id.tv_left_book:
                startActivity(new Intent(getActivity(), BuyListActivity.class));
                break;
            case R.id.tv_left_apply:
                if (login_name != "") {
                    startActivity(new Intent(AppContext.getContext(), ApplyActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.tv_left_cert:
                startActivity(new Intent(getActivity(), CertificateSearchActivity.class));
                break;
            case R.id.tv_left_teacher:
                startActivity(new Intent(getActivity(), MidiTeacherActivity.class));
                break;
            case R.id.tv_right_exam:
                if (login_name != "") {
                    startActivity(new Intent(getActivity(), ExamListActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.tv_right_book:
                startActivity(new Intent(getActivity(), BuyListActivity.class));
                break;
            case R.id.tv_right_apply:
                if (login_name != "") {
                    startActivity(new Intent(AppContext.getContext(), ApplyActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }

                break;
            case R.id.tv_right_cert:
                startActivity(new Intent(getActivity(), CertificateSearchActivity.class));

                break;
            case R.id.tv_right_teacher:
                startActivity(new Intent(getActivity(), MidiTeacherActivity.class));
                break;
        }
    }
}
